package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.base.ConditionDTO;
import com.hang.model.dto.request.ModuleModelDTO;
import com.hang.model.dto.request.ModuleQueryDTO;
import com.hang.model.dto.response.ModuleQueryListVO;
import com.hang.model.vo.ModuleDetailVO;
import com.hang.service.IModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "模块管理接口")
@RequestMapping("module")
@RestController
@Slf4j
public class ModuleController {
    @Resource
    private IModuleService moduleService;

    @ApiOperation(value = "新增项目模块")
    @PostMapping("create")
    public Result<Object> createItemModule(@RequestBody @Valid ModuleModelDTO moduleModelDTO){
        try {
            moduleService.createItemModule(moduleModelDTO);
            return Result.ok().message("创建成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "修改项目模块")
    @PostMapping("update")
    public Result<Object> editItemModule(
            @RequestBody @Valid ModuleModelDTO moduleModelDTO
    ){
        try {
            moduleService.editItemModule(moduleModelDTO);
            return Result.ok().message("更新成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "删除项目模块")
    @DeleteMapping("delete")
    public Result<Object> deleteItemModule(
            @RequestParam("moduleId") Long moduleId
    ){
        try {
            moduleService.deleteModule(moduleId);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "批量删除项目模块")
    @DeleteMapping("deleteModuleList")
    public Result<Object> deleteItemModuleList(
            @RequestParam("moduleIdList") List<Long> moduleIdList
    ){
        try {
            moduleService.deleteModuleList(moduleIdList);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "查询模块列表")
    @PostMapping("getModuleListByCondition")
    public Result<ModuleQueryListVO> getModuleListByCondition(
            @RequestBody @Valid ModuleQueryDTO moduleQueryDTO
    ){
        ModuleQueryListVO moduleQueryListVO = moduleService.getModuleListByCondition(moduleQueryDTO);
        return Result.ok(moduleQueryListVO);
    }

    @ApiOperation(value = "查询模块详情")
    @GetMapping("getModuleDetail")
    public Result<ModuleDetailVO> getModuleDetail(@RequestParam("moduleId") Long moduleId){
        ModuleDetailVO moduleDetailVO = moduleService.getModuleDetailById(moduleId);
        return Result.ok(moduleDetailVO);
    }
}